#include "WindowManager.h"
#include "PeopleWindow.h"
#include "RegisterWindow.h"
#include "NotifWindow.h"
#include "KeyGetWindow.h"
#include "FindWindow.h"
#include "ChoiceWindow.h"
WindowManager* WindowManager::mInstance=0;
WindowManager::WindowManager(int& argc,char** argv):QApplication(argc,argv)
{
    mInstance=this;
}
BaseWindow* WindowManager::ShowWindow(WindowType type, void *data)
{


    BaseWindow* win=GetWindow(type);
    if(win)
        return 0;
    switch(type)
    {
        case LOGINWINDOW:
            win=new LoginWindow();
            break;
        case CHATWINDOW:
            win=new ChatWindow();
            break;

        case PEOPLEWINDOW:
             win=new PeopleWindow();
             break;

        case REGISTERWINDOW:
             win= new RegisterWindow();
             break;

        case NOTIFWINDOW:
            win= new NotifWindow();
            if(data)
                ((NotifWindow*)win)->SetText(*(QString*)data);
            break;
        case KEYGETWINDOW:
            win= new KeyGetWindow();

           break;
    case CHOICE_WINDOW:
         win= new ChoiceWindow();


      break;
    case FINDWINDOW:
         win= new FindWindow();


      break;


    };
    if(win)
    {
        mWindowList.push_back(win);
        win->show();
    }
    return win;
}
BaseWindow* WindowManager::GetWindow(WindowType type)
{
    list<BaseWindow*>::iterator it=mWindowList.begin();
    while(it!=mWindowList.end())
    {

        switch(type)
        {
        case CHATWINDOW:
            if(dynamic_cast<ChatWindow*>(*it))
                return *it;
            break;
        case LOGINWINDOW:
                if(dynamic_cast<LoginWindow*>(*it))
                    return *it;

              break;
        case CHOICE_WINDOW:
            if(dynamic_cast<ChoiceWindow*>(*it))
                return *it;

          break;

            case PEOPLEWINDOW:
               if(dynamic_cast<PeopleWindow*>(*it))
                  return *it;
               break;

            case REGISTERWINDOW:
                if(dynamic_cast<RegisterWindow*>(*it))
                   return *it;
                break;

            case NOTIFWINDOW:
                if(dynamic_cast<NotifWindow*>(*it))
                 return *it;
                break;
        case KEYGETWINDOW:
            if(dynamic_cast<KeyGetWindow*>(*it))
             return *it;
            break;
        case FINDWINDOW:
            if(dynamic_cast<FindWindow*>(*it))
             return *it;
            break;

        }
       it++;
    }
    return 0;
}

void WindowManager::DoCommand(WindowType type,QString command,void* data)
{
    LoginWindow* login=0;
    PeopleWindow* people=0;
    RegisterWindow* reg=0;
    NotifWindow* notif=0;
    ChatWindow* chat=0;
    list<BaseWindow*>::iterator it=mWindowList.begin();
    while(it!=mWindowList.end())
    {

        switch(type)
        {
            case LOGINWINDOW:
                if(login=dynamic_cast<LoginWindow*>(*it))
                {
                    if(command=="set_def_id")
                        login->SetUserId(*(QString*)data);
                }
              break;

            case PEOPLEWINDOW:
               if(people=dynamic_cast<PeopleWindow*>(*it))
               {
               }
               break;

            case CHATWINDOW:
               if(chat=dynamic_cast<ChatWindow*>(*it))
               {
               }
               break;

            case REGISTERWINDOW:
                if(reg=dynamic_cast<RegisterWindow*>(*it))
                {

                }
                break;

            case NOTIFWINDOW:
                if(notif=dynamic_cast<NotifWindow*>(*it))
                {
                }
                 break;
        }
       it++;
    }
}

bool WindowManager::event(QEvent * ev)
{
    if(ev->type()==QEvent::Timer)
    {
        list<BaseWindow*>::iterator it=mWindowList.begin();
        while(it!=mWindowList.end())
        {
            if((*it)->isHidden())
            {
              delete (*it);
              mWindowList.erase(it);
              break;
            }
            it++;
        }
    }

    return QApplication::event(ev);
}
void WindowManager::RemoveWindow(BaseWindow* win)
{
    list<BaseWindow*>::iterator it=mWindowList.begin();
    while(it!=mWindowList.end())
    {
        if(*it==win)
        {
            win->deleteLater();
            mWindowList.erase(it);
            break;
        }
        it++;
   }
}

void WindowManager::CloseWindow(WindowType type)
{
    list<BaseWindow*>::iterator it=mWindowList.begin();
    while(it!=mWindowList.end())
    {
        bool hide=false;
        switch(type)
        {
            case LOGINWINDOW:
                if(dynamic_cast<LoginWindow*>(*it))
                 hide=true;
              break;

            case PEOPLEWINDOW:
               if(dynamic_cast<PeopleWindow*>(*it))
                 hide=true;
               break;

            case REGISTERWINDOW:
                if(dynamic_cast<RegisterWindow*>(*it))
                    hide=true;
                break;

            case NOTIFWINDOW:
                if(dynamic_cast<NotifWindow*>(*it))
                    hide=true;
                 break;
            case KEYGETWINDOW:
                if(dynamic_cast<KeyGetWindow*>(*it))
                    hide=true;
                 break;
        case CHOICE_WINDOW:
            if(dynamic_cast<ChoiceWindow*>(*it))
                hide=true;
             break;
        case FINDWINDOW:
            if(dynamic_cast<FindWindow*>(*it))
                hide=true;
             break;
        }
        if(hide)
        {
            (*it)->close();
             break;
        }
        it++;
    }

}

WindowManager::~WindowManager()
{
    mInstance=0;
    list<BaseWindow*>::iterator it=mWindowList.begin();
    while(it!=mWindowList.end())
    {
        delete *it;
        it++;
    }
}

